package com.netease.yunxin.kit.chatkit.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.jeremyliao.liveeventbus.LiveEventBus
import com.netease.yunxin.kit.chatkit.ui.databinding.FragmentGiftLayoutBinding
import com.netease.yunxin.kit.chatkit.ui.dialog.GiftDialog
import com.netease.yunxin.kit.chatkit.ui.model.Gift
import com.netease.yunxin.kit.chatkit.ui.page.adapter.GiftAdapter

class GiftFragment : Fragment() {
    private lateinit var adapter: GiftAdapter
    lateinit var mDataBinding: FragmentGiftLayoutBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mDataBinding = FragmentGiftLayoutBinding.inflate(inflater, container, false)
        initView()
        return mDataBinding.root
    }

    private fun initView(){
        val gifts = arguments?.getParcelableArrayList<Gift>("GIFT")?:return
        adapter = GiftAdapter(gifts)
        mDataBinding.recyclerView.adapter = adapter
        mDataBinding.recyclerView.layoutManager = object : GridLayoutManager(requireContext(),4){
            override fun canScrollHorizontally(): Boolean {
                return false
            }

            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        adapter.setOnItemClickListener { _, _, position ->
            GiftDialog.clickItem = gifts[position]
            LiveEventBus.get<Any>("GIFT_NOTIFY").post(null)
        }
        LiveEventBus.get<Any>("GIFT_NOTIFY").observe(viewLifecycleOwner){
            adapter?.notifyDataSetChanged()
        }
    }
}